//
//  HomeFlowBannerTopicCell.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/23.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "HomeFlowBannerTopicCell.h"
#import "GYRollingNoticeView.h"
#import "HomeBannerTopicNoticeCell.h"

typedef NS_ENUM(NSInteger, noticeTag) {
    noticeTag1 = 1,
    noticeTag2 = 2,
    noticeTag3 = 3,
    noticeTag4 = 4,
};

@interface HomeFlowBannerTopicCell ()<SDCycleScrollViewDelegate,GYRollingNoticeViewDataSource, GYRollingNoticeViewDelegate>

@property (nonatomic, strong) GYRollingNoticeView *noticeView1;
@property (nonatomic, strong) GYRollingNoticeView *noticeView2;
@property (nonatomic, strong) GYRollingNoticeView *noticeView3;
@property (nonatomic, strong) GYRollingNoticeView *noticeView4;

@property (nonatomic, strong) NSMutableArray <HomeADItemEventModel *>*notiArray1;
@property (nonatomic, strong) NSMutableArray <HomeADItemEventModel *>*notiArray2;
@property (nonatomic, strong) NSMutableArray <HomeADItemEventModel *>*notiArray3;
@property (nonatomic, strong) NSMutableArray <HomeADItemEventModel *>*notiArray4;

@end

@implementation HomeFlowBannerTopicCell

+ (CGFloat)calculateHeight {
    return 0.55882*(TTScreenWidth - 26) + 10;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.topicControl pp_clipCorners:UIRectCornerAllCorners radius:6];
        [self.bannerControl pp_clipCorners:UIRectCornerAllCorners radius:6];

        [self.communityControl1 pp_clipCorners:UIRectCornerAllCorners radius:4];
        [self.communityControl2 pp_clipCorners:UIRectCornerAllCorners radius:4];
        [self.communityControl3 pp_clipCorners:UIRectCornerAllCorners radius:4];
        [self.communityControl4 pp_clipCorners:UIRectCornerAllCorners radius:4];

    });
    
    [self.communityControl1 addSubview:self.noticeView1];
    [self.communityControl2 addSubview:self.noticeView2];
    [self.communityControl3 addSubview:self.noticeView3];
    [self.communityControl4 addSubview:self.noticeView4];
    
    [self.noticeView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    [self.noticeView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    [self.noticeView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    [self.noticeView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];

}

- (void)awakeFromNib {
    [super awakeFromNib];

    [self.bannerControl addSubview:self.bannerView];
    
    [self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0);
    }];
    
    [self.topicControl addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
        [[StatisticManager shareManager]insertBrowseUserActiveQuantityWithType:@"10"];
        if (self.HomeFlowTopicBlock) {
            self.HomeFlowTopicBlock(HomeFlowTopicActionType_more, -1);
        }
    }];
    
}

- (void)fillCellWithModel:(HomeADItemModel *)model {
    
    self.model = model;
    self.bannerView.imageURLStringsGroup = model.photos;

    //给话题赋值
    NSArray *extList = model.sysAdvertisement.excList;
    
    self.communityControl1.hidden = YES;
    self.communityControl2.hidden = YES;
    self.communityControl3.hidden = YES;
    self.communityControl4.hidden = YES;

    if (extList.count >=4) {
        self.communityLab4.text = model.sysAdvertisement.excList[3].sellerName;
        self.communityControl4.hidden = NO;
    }
    
    if (extList.count >=3) {
        self.communityLab3.text = model.sysAdvertisement.excList[2].sellerName;
        self.communityControl3.hidden = NO;
    }
    
    if (extList.count >=2) {
        self.communityLab2.text = model.sysAdvertisement.excList[1].sellerName;
        self.communityControl2.hidden = NO;
    }
    
    if (extList.count >=1) {
        self.communityLab1.text = model.sysAdvertisement.excList[0].sellerName;
        self.communityControl1.hidden = NO;
    }
    
    self.notiArray1 = model.sysAdvertisement.excList.mutableCopy;
    self.notiArray2 = [self rebuildArrayWithOffset:1 AndArray:self.notiArray1.mutableCopy];
    self.notiArray3 = [self rebuildArrayWithOffset:1 AndArray:self.notiArray2.mutableCopy];
    self.notiArray4 = [self rebuildArrayWithOffset:1 AndArray:self.notiArray3.mutableCopy];

    [self.noticeView1 reloadDataAndStartRoll];
    [self.noticeView2 reloadDataAndStartRoll];
    [self.noticeView3 reloadDataAndStartRoll];
    [self.noticeView4 reloadDataAndStartRoll];

}

- (NSMutableArray <HomeADItemEventModel *>*)rebuildArrayWithOffset:(NSInteger)offset AndArray:(NSMutableArray <HomeADItemEventModel *>*)array {
    
    if (array.count > 0) {
        for (int i = 0; i < offset; i++) {
            HomeADItemEventModel *model = array.firstObject;
            [array removeObjectAtIndex:0];
            [array addObject:model];
        }
    }
    return array;
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    [[StatisticManager shareManager]insertBrowseUserActiveQuantityWithType:@"9"];
    if (self.CycleImgClickedBlock) {
        self.CycleImgClickedBlock(index);
    }
}

- (NSInteger)numberOfRowsForRollingNoticeView:(GYRollingNoticeView *)rollingView {
    return self.model.sysAdvertisement.excList.count;
}

- (__kindof GYNoticeViewCell *)rollingNoticeView:(GYRollingNoticeView *)rollingView cellAtIndex:(NSUInteger)index {
    
    switch (rollingView.tag) {
        case noticeTag1:{
            HomeBannerTopicNoticeCell *cell = [rollingView dequeueReusableCellWithIdentifier:@"HomeBannerTopicNoticeCell"];
            [cell fillCellWithModel:self.notiArray1[index]];

            return cell;
        } break;
        case noticeTag2:{
            HomeBannerTopicNoticeCell *cell = [rollingView dequeueReusableCellWithIdentifier:@"HomeBannerTopicNoticeCell"];
            [cell fillCellWithModel:self.notiArray2[index]];

            return cell;
        } break;
        case noticeTag3:{
            HomeBannerTopicNoticeCell *cell = [rollingView dequeueReusableCellWithIdentifier:@"HomeBannerTopicNoticeCell"];
            [cell fillCellWithModel:self.notiArray3[index]];

            return cell;
        } break;
        case noticeTag4:{
            HomeBannerTopicNoticeCell *cell = [rollingView dequeueReusableCellWithIdentifier:@"HomeBannerTopicNoticeCell"];
            [cell fillCellWithModel:self.notiArray4[index]];

            return cell;
        } break;
            
        default:{
            HomeBannerTopicNoticeCell *cell = [rollingView dequeueReusableCellWithIdentifier:@"HomeBannerTopicNoticeCell"];
            [cell fillCellWithModel:self.model.sysAdvertisement.excList[index]];
            return cell;
        } break;
    }

}

//轮播的点击事件
- (void)didClickRollingNoticeView:(GYRollingNoticeView *)rollingView forIndex:(NSUInteger)index {
    [[StatisticManager shareManager]insertBrowseUserActiveQuantityWithType:@"10"];
    NSInteger selIndex = 0;
    switch (rollingView.tag) {
        case noticeTag1:{
            selIndex = self.notiArray1[index].index;
        } break;
        case noticeTag2:{
            selIndex = self.notiArray2[index].index;
        } break;
        case noticeTag3:{
            selIndex = self.notiArray3[index].index;
        } break;
        case noticeTag4:{
            selIndex = self.notiArray4[index].index;
        } break;
        default: break;
    }
            
        if (self.HomeFlowTopicBlock) {
            self.HomeFlowTopicBlock(HomeFlowTopicActionType_index, selIndex);
        }
}

- (SDCycleScrollView *)bannerView {
    if (!_bannerView) {
        _bannerView = [[SDCycleScrollView alloc] init];
        _bannerView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
        _bannerView.autoScrollTimeInterval = 5;
        _bannerView.backgroundColor = UIColorHex(0xf1f1f1);
        _bannerView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
        _bannerView.delegate = self;
    }
    return _bannerView;
}

- (GYRollingNoticeView *)noticeView1 {
    
    if (!_noticeView1) {
        _noticeView1 = [[GYRollingNoticeView alloc]init];
        _noticeView1.tag = noticeTag1;
        _noticeView1.dataSource = self;
        _noticeView1.delegate = self;
        _noticeView1.stayInterval = 3;
        _noticeView1.backgroundColor = UIColor.clearColor;
        [_noticeView1 registerClass:[HomeBannerTopicNoticeCell class] forCellReuseIdentifier:@"HomeBannerTopicNoticeCell"];
    }
    return _noticeView1;
}

- (GYRollingNoticeView *)noticeView2 {
    
    if (!_noticeView2) {
        _noticeView2 = [[GYRollingNoticeView alloc]init];
        _noticeView2.tag = noticeTag2;
        _noticeView2.dataSource = self;
        _noticeView2.delegate = self;
        _noticeView2.stayInterval = 3;
        _noticeView2.backgroundColor = UIColor.clearColor;
        [_noticeView2 registerClass:[HomeBannerTopicNoticeCell class] forCellReuseIdentifier:@"HomeBannerTopicNoticeCell"];
    }
    return _noticeView2;
}

- (GYRollingNoticeView *)noticeView3 {
    
    if (!_noticeView3) {
        _noticeView3 = [[GYRollingNoticeView alloc]init];
        _noticeView3.tag = noticeTag3;
        _noticeView3.dataSource = self;
        _noticeView3.delegate = self;
        _noticeView3.stayInterval = 3;
        _noticeView3.backgroundColor = UIColor.clearColor;
        [_noticeView3 registerClass:[HomeBannerTopicNoticeCell class] forCellReuseIdentifier:@"HomeBannerTopicNoticeCell"];
    }
    return _noticeView3;
}

- (GYRollingNoticeView *)noticeView4 {
    
    if (!_noticeView4) {
        _noticeView4 = [[GYRollingNoticeView alloc]init];
        _noticeView4.tag = noticeTag4;
        _noticeView4.dataSource = self;
        _noticeView4.delegate = self;
        _noticeView4.stayInterval = 3;
        _noticeView4.backgroundColor = UIColor.clearColor;
        [_noticeView4 registerClass:[HomeBannerTopicNoticeCell class] forCellReuseIdentifier:@"HomeBannerTopicNoticeCell"];
    }
    return _noticeView4;
}



@end
